package com.xioz.android;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;

/**
 * SurvivalGameScreen Class
 * This is a subclass of WordGameScreen, simply used to show
 * a particular game in which the player scores points before 
 * filling the entire screen 
 * 
 * @author Zong Xiong
 */
public class SurvivalGameScreen extends WordGameScreen {
	
/***************************************************
 * Fields
 ***************************************************/
	
	// time limit between each additional three times are added
	private float TILE_TIME_LIMIT = 10000;
	// used to store and load scores
	private Preferences prefs;
	// tracks score placement after goal is met
	private int scorePlacement = 0;
	
/***************************************************
 * Public Methods
 ***************************************************/
	
	/**
	 * Default Constructor
	 * calls superclass constructor then own initialization
	 */
	public SurvivalGameScreen() {
		super();
		survivalGameInitialization();
	}

	/**
	 * Constructor with parent Game
	 * calls superclass constructor then own initialization
	 * @param inGame , parent game
	 */
	public SurvivalGameScreen(Game inGame) {
		super();
		survivalGameInitialization();
	}

/***************************************************
 * Protected Methods
 ***************************************************/
	
	/**
	 * Modified draw method
	 * Adds a line of text for score display
	 */
	protected void draw() {
		super.draw();
		printBoard("Points:" + pointScore , 20, 79);
	}
	
	/**
	 * Modified update method
	 * Tracks if goals are met for game and if so ends game
	 */
	protected void update() {
		super.update();
		
		// if time limit has been met, we add three more tiles
		if (gameTimer.getTime() >= TILE_TIME_LIMIT ) {
			super.addTiles(3);
			gameTimer.reset();
		}
		
		/* if the game board is over-filled, we mark this as a lost
		 * ending the game. At a lost, we check if the score earned
		 * charts on the score board. If so we update it*/
		if (!super.notFull) {
			super.loseGame();
			
			// check if score charted
			for (int i=1; i<6 && scorePlacement == 0; i++) {
				if (pointScore > prefs.getLong("Place" + i + "Score")) {
					scorePlacement = i;
					shiftScores(i);
					prefs.putLong("Place" + i + "Score", pointScore);
				}
			}
			
			// if score charted, prompt user for name
			if (scorePlacement > 0) {
				super.enterHighScore();
				prefs.putString("Place" + scorePlacement + "Name", super.getScoreName() );
				prefs.flush();
			}
		}
	}
	
/***************************************************
 * Private Methods
 ***************************************************/
	
	/**
	 * ShiftScores
	 * shifts scores downward from scorePlacement
	 * @param scorePlacement, place acquired by player
	 */
	private void shiftScores(int scorePlacement) {
		for (int i = 5; i > scorePlacement; i--) {
			prefs.putLong("Place" + (i) + "Score", prefs.getLong("Place" + (i-1) + "Score"));
			prefs.putString("Place" + (i) + "Name", prefs.getString("Place" + (i-1) + "Name"));
		}
	}
	
	/**
	 * survivalGameInitialization
	 * initializes a survival game
	 */
	private void survivalGameInitialization() {
		// load score board
		prefs = Gdx.app.getPreferences("SurvivalWordGame");
	}
}
